/**
 * FlexiGridUtil.java Create on 2012-3-2上午10:17:21 Copyright (c) 2012 by e_trans.
 */
package com.etrans.web.gps.util.grid;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * FlexGrid 参数解析工具
 * User: E-trans
 * Date: 2017/6/9
 * Time: 9:54
 * To change this template use File | Settings | File Templates.
 */
public class FlexiGridUtil {

	/**
	 * 解析FlexiGrid 控件的JOSN 参数 为ParamMap对象
	 * @param json
	 * @return
	 */
	public static ParamMap  parseJSONParam(String json) {    
		if (StringUtils.isEmpty(json)) {
			return null;
		}
		ParamMap paramMap = null;
		try {
			Map<String, Object> params = JSON.parseObject(json, Map.class);
			if (params != null) {
				paramMap = new ParamMap();
				Map<String, Object> whereParam = new HashMap<>();
				if(null!=params.get("page"))whereParam.put(ParamKey.PAGE, Integer.parseInt(params.get("page").toString()));
				if(null!=params.get("pageSize"))whereParam.put(ParamKey.PAGE_SIZE, Integer.parseInt(params.get("pageSize").toString()));
				if(null!=params.get("sortName"))whereParam.put(ParamKey.ORDER_NAME, params.get("sortName").toString());
				if(null!=params.get("sortOrder"))whereParam.put(ParamKey.ORDER_SORT, params.get("sortOrder").toString());
				if(null!=params.get("page"))whereParam.put("fromNumber", (Integer.parseInt(params.get("page").toString())-1)*Integer.parseInt(params.get("pageSize").toString()));
				JSONArray whereParamsList = (JSONArray)params.get("whereparams");
				if (whereParamsList != null) {
					List<HashMap<String, Object>> listMap =  handleJSONArray(whereParamsList);
					for (HashMap<String, Object> m : listMap) {
						whereParam.put(m.get("name").toString(), m.get("value"));
					}
				}
				// 排序
				Map<String, String> orderParam = new HashMap<>();
				orderParam.put(params.get("sortName").toString() + "Order", params.get("sortOrder").toString());
				paramMap.putWhereParam(whereParam);
				paramMap.putOrderParam(orderParam);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paramMap;
	}
	
	/**
	 * 
	 * @param jsonArray
	 * @return
	 */
	private  static List<HashMap<String, Object>> handleJSONArray(JSONArray jsonArray){
        List list = new ArrayList();
        for (Object object : jsonArray) {
            JSONObject jsonObject = (JSONObject) object;
            HashMap map = new HashMap<String, Object>();
            for (Map.Entry entry : jsonObject.entrySet()) {
                if(entry.getValue() instanceof  JSONArray){
                    map.put((String)entry.getKey(), handleJSONArray((JSONArray)entry.getValue()));
                }else{
                    map.put((String)entry.getKey(), entry.getValue());
                }
            }
            list.add(map);
        }
        return list;
    }

	/**
	 * 解析FlexiGrid 控件的JOSN 参数 为ParamMap对象，由储存过程调用 
	 * @param json
	 * @return
	 */
	public static Map<String, Object> parseJSONParamForProcedure(String json) {
		if (StringUtils.isEmpty(json)) {
			return null;
		}
		Map<String, Object> params = JSON.parseObject(json, Map.class);
		Map<String, Object> setParam = new HashMap<>();
		try {
			if (params != null) {
				setParam.put(ParamKey.PAGE, params.get("page").toString());
				setParam.put(ParamKey.PAGE_SIZE, params.get("pageSize").toString());
				List<HashMap<String, Object>> whereParamsLst = (List<HashMap<String, Object>>) params.get("whereparams");

				if (whereParamsLst != null) {
					for (HashMap<String, Object> m : whereParamsLst) {
						setParam.put(m.get("name").toString(), m.get("value"));
					}
				}
				String sortName = params.get("sortName").toString();
				String sortOrder = params.get("sortOrder").toString();
				setParam.put("@sortName", sortName);
				setParam.put("@sortOrder", sortOrder);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return setParam;
	}

	/**
	 * 解析FlexiGrid 控件的JOSN参数为Map对象
	 * @param json
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> parseParam(String json) {
		if (StringUtils.isEmpty(json)) {
			return null;
		}
		Map<String, Object> paramMap = null;
		try {
			Map<String, Object> params = JSON.parseObject(json, Map.class);
			if (params != null) {
				paramMap = new HashMap<>();
				//分页
				String page = params.get("page") == null ? "1" : params.get("page").toString();
				String pageSize = params.get("pageSize") == null ? "10" : params.get("pageSize").toString();
				paramMap.put(ParamKey.PAGE, Integer.parseInt(page));
				paramMap.put(ParamKey.PAGE_SIZE, Integer.parseInt(pageSize));
				Integer fromRow = RowNumUtil.getFromRowNum(page, pageSize);
				Integer toRow = RowNumUtil.getToRowNum(page, pageSize);
				paramMap.put("fromRow", fromRow);
				paramMap.put("toRow", toRow);
				//排序
				paramMap.put(ParamKey.ORDER_NAME, params.get("sortName").toString());
				paramMap.put(ParamKey.ORDER_SORT, params.get("sortOrder").toString());
				//页面查询条件
				List<HashMap<String, Object>> whereParamsLst = (List<HashMap<String, Object>>) params.get("whereparams");
				if (whereParamsLst != null) {
					for (HashMap<String, Object> m : whereParamsLst) {
						paramMap.put(m.get("name").toString(), m.get("value"));
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paramMap;
	}

	
	/**
	 * 解析FlexiGrid 控件的JOSN中display和name为List对象
	 *
	 * @param json
	 * @return
	 */
	public static HashMap<String, Object> parseJSONToList(String json) {
		List<Map<String, Object>> params = JSON.parseObject(json, List.class);
		String[] titleArray = new String[params.size()];
		String[] columnArray = new String[params.size()];
		String[] widthArray = new String[params.size()];
		for (int i=0; i < params.size(); i ++) {
			titleArray[i] = String.valueOf(params.get(i).get("display"));
			columnArray[i] = String.valueOf(params.get(i).get("name"));
			widthArray[i] = String.valueOf(params.get(i).get("width"));
		}
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("titleArray", titleArray);
		map.put("columnArray", columnArray);
		map.put("widthArray", widthArray);
		return map;
	}
}
